package com.njitzx.fileupload.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);

        // 设置键（key）序列化器，通常用 StringRedisSerializer 来处理 String 类型的键
        redisTemplate.setKeySerializer(new StringRedisSerializer());

        // 设置值（value）序列化器，这里使用 GenericJackson2JsonRedisSerializer，支持将对象转为 JSON 格式
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        // 设置 Hash 键（hash key）序列化器
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());

        // 设置 Hash 值（hash value）序列化器
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        return redisTemplate;
    }


}
